package se.teamscape.server.rpc;

import net.customware.gwt.dispatch.server.ActionHandler;
import net.customware.gwt.dispatch.server.ExecutionContext;
import net.customware.gwt.dispatch.shared.ActionException;
import se.teamscape.server.db.Datastore;
import se.teamscape.shared.rpc.GetModel;
import se.teamscape.shared.rpc.GetModelResult;

import com.google.inject.Inject;

/**
 * Generic handler for fetching any model implementation.
 * 
 * @author Andreas Borglin
 * 
 */
public class GetModelHandler implements ActionHandler<GetModel, GetModelResult> {

    private final Datastore mDatastore;

    @Inject
    public GetModelHandler(Datastore datastore) {
        this.mDatastore = datastore;
    }

    @Override
    public GetModelResult execute(GetModel arg0, ExecutionContext arg1) throws ActionException {

        return new GetModelResult(mDatastore.getModel(arg0.getId(), arg0.getClassName()));

    }

    @Override
    public Class<GetModel> getActionType() {
        return GetModel.class;
    }

    @Override
    public void rollback(GetModel arg0, GetModelResult arg1, ExecutionContext arg2) throws ActionException {

    }

}
